import React from 'react';
import ProForm, {ProFormText} from "@ant-design/pro-form";
import {message, Modal} from "antd";
import {updatePassword} from "@/services/user";

const Create = (props) => {

  // 从父组件传过来属性
  const {actionRef, uid, isShowUpdatePwModal, isPwModalVisible} = props;

  // 执行修改密码
  const handleUpdatePw = (pw) => {

    const data = {
      id: uid,
      password: pw.password
    }
    const response = updatePassword(data);
    if (response.status === undefined) {
      actionRef.current.reload()
      message.success('修改密码成功')
      isShowUpdatePwModal(false)
    }
  }

  return (
    <Modal title="修改密码"
           visible={isPwModalVisible}
           onCancel={() => isShowUpdatePwModal(false)}
           footer={false}
           destroyOnClose={true}
    >
      <ProForm
        onFinish={(value) => handleUpdatePw(value)}
        submitter={{
          searchConfig: {
            submitText: '提交',
          },
          submitButtonProps: {
            size: 'middle',
            style: {
              width: '100%',
            },
          },
        }}
      >
        <ProFormText
          name="id"
          hidden={true}
        />
        <ProFormText.Password
          name="password"
          label="密码"
          placeholder="请输入密码"
          rules={[
            {
              required: true,
              message: '请输入密码!',
            },
            {
              min: 6,
              message: '密码 最低6位'
            },
            {
              max: 12,
              message: '密码 最多12位'
            }
          ]}
        />
      </ProForm>
    </Modal>
  );
};

export default Create;
